package com.pulse.common.utils.sor.make;

import com.pulse.common.utils.sor.Parts;
import com.pulse.common.utils.sor.domain.EventVo;
import com.pulse.common.utils.sor.domain.SorInfo;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * @ClassName KeyEventsMake
 * @Description
 * @Author 陆帅
 * @Date 2025/3/14 21:49
 */
public class KeyEventsMake {

    public static final String KEY_EVENTS_BLOCK_ID = "KeyEvents\0";

    public static ByteBuffer make(SorInfo info) {
        ByteBuffer buffer = ByteBuffer.allocate(256);
        buffer.order(ByteOrder.LITTLE_ENDIAN);

        // 块ID
        buffer.put(KEY_EVENTS_BLOCK_ID.getBytes());
        // 事件数量
        int numEvents = info.getKeyEvents().getNumEvents();
        buffer.putShort((short) numEvents);

        double factor = 1e-4 * Parts.Sol / Double.parseDouble (info.getFxdParams().getIndex());

        for(int i = 0; i < numEvents; i++){
            EventVo event = info.getKeyEvents().getEvents().get(i);
            // 事件号
            int eventNum = i+1;
            buffer.putShort((short)eventNum);
            // 事件传播时间
            Double eventPropagationTime =  Double.parseDouble(event.getDistance()) / factor;
            buffer.putInt((int) Math.round(eventPropagationTime));
            // 斜率（事件段平均衰耗）
            Double slope = Double.parseDouble(event.getSlope()) * 1000;
            buffer.putShort((short) Math.round(slope));
            // 插入损耗
            Double spliceLoss = Double.parseDouble(event.getSpliceLoss()) * 1000;
            buffer.putShort(spliceLoss.shortValue());
            // 反射损耗
            Double reflLoss = Double.parseDouble(event.getReflLoss()) * 1000;
            buffer.putInt((int) Math.round(reflLoss));
            // 事件类型
            String type = event.getType() == null ? "0A9999" : event.getType().substring(0,6);
            buffer.put(type.getBytes());
            String type2 = event.getType() == null ? "LS" : event.getType().substring(6,8);
            buffer.put(type2.getBytes());
            // 上一事件结束时间
            String endOfPrevStr = event.getEndOfPrev() == null ? "0" : event.getEndOfPrev();
            Double endOfPrev = Double.parseDouble(endOfPrevStr) / factor;
            buffer.putInt((int) Math.round(endOfPrev));
            // 当前时间开始时间
            String startOfCurrStr = event.getStartOfCurr() == null ? "0" : event.getStartOfCurr();
            Double startOfCurr = Double.parseDouble(startOfCurrStr) / factor;
            buffer.putInt((int) Math.round(startOfCurr));
            // 当前时间结束时间
            String endOfCurrStr = event.getEndOfCurr() == null ? "0" : event.getEndOfCurr();
            Double endOfCurr = Double.parseDouble(endOfCurrStr) / factor;
            buffer.putInt(endOfCurr.intValue());
            // 下一个事件开始时间
            String startOfNextStr = event.getStartOfNext() == null ? "0" : event.getStartOfNext();
            Double startOfNext = Double.parseDouble(startOfNextStr) / factor;
            buffer.putInt((int) Math.round(startOfNext));
            // 事件的峰值时间
            String peakStr = event.getPeak() == null ? "0" : event.getPeak();
            Double peak = Double.parseDouble(peakStr) / factor;
            buffer.putInt(peak.intValue());
            // 备注
            String comments = event.getComments() == null ? "\0" : event.getComments() + "\0";
            buffer.put(comments.getBytes());
        }

        // 总损耗（全程损耗）
        Double totalLoss = info.getKeyEvents().getSummary().getTotalLoss() == null ? 0d : info.getKeyEvents().getSummary().getTotalLoss() * 1000;
        buffer.putInt((int) Math.round(totalLoss));
        // 损耗起始时间
        Double lossStart = info.getKeyEvents().getSummary().getLossStart() == null ? 0d : info.getKeyEvents().getSummary().getLossStart() / factor;
        buffer.putInt(lossStart.intValue());
        // 损耗结束时间(全程长度)
        Double lossEnd = info.getKeyEvents().getSummary().getLossEnd() == null ? 0d : info.getKeyEvents().getSummary().getLossEnd() / factor;
        buffer.putInt((int) Math.round(lossEnd));
        // 光回波损耗 (ORL)
        Double orl = info.getKeyEvents().getSummary().getOrl() == null ? 0d : info.getKeyEvents().getSummary().getOrl() * 1000;
        buffer.putShort((short) Math.round(orl));
        // 光回波损耗起始时间
        Double orlStart = info.getKeyEvents().getSummary().getOrlStart() == null ? 0d : info.getKeyEvents().getSummary().getOrlStart() / factor;
        buffer.putInt(orlStart.intValue());
        // 光回波损耗结束时间
        Double orlEnd = info.getKeyEvents().getSummary().getOrlFinish() == null ? 0d : info.getKeyEvents().getSummary().getOrlFinish() / factor;
        buffer.putInt((int) Math.round(orlEnd));

        return buffer;
    }
}
